package org.example.classtopia.Controller;

import io.swagger.v3.oas.annotations.Parameter;
import org.example.classtopia.Common.Result.Result;
import org.example.classtopia.Model.dto.*;
import org.example.classtopia.Service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.example.classtopia.Utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Tag(name = "用户管理")
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<?> register(@RequestBody RegisterDTO registerDTO, HttpServletRequest request) {
        return userService.Register(registerDTO, request);
    }

    @Operation(summary = "用户密码登录")
    @PostMapping("/loginByPassword")
    public Result<?> loginByPassword(@RequestBody LoginByPasswordDTO loginByPasswordDTO, HttpServletRequest request) {
        return userService.LoginByPassword(loginByPasswordDTO, request);
    }

    @Operation(summary = "用户验证码登录")
    @PostMapping("/loginByCode")
    public Result<?> loginByCode(@RequestBody LoginByCodeDTO loginByCodeDTO,HttpServletRequest request){
        return userService.LoginByCode(loginByCodeDTO,request);
    }

    @Operation(summary = "修改基本信息")
    @PostMapping("/updateCommonData")
    public Result<?> updateCommonData(@RequestBody UpdateCommonDataDTO updateCommonDataDTO, HttpServletRequest request) {
        return userService.UpdateCommonData(updateCommonDataDTO, request);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/updatePassword")
    public Result<?> updatePassword(@RequestBody UpdatePasswordDTO updatePasswordDTO, HttpServletRequest request){
        return userService.UpdatePassword(updatePasswordDTO, request);
    }

    @Operation(summary = "忘记密码重新设置")
    @PostMapping("/resetPassword")
    public Result<?> resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO, HttpServletRequest request){
        return userService.ResetPassword(resetPasswordDTO,request);
    }

    @Operation(summary = "发送验证码")
    @PostMapping("/code")
    public Result<?> getCode(@RequestBody EmailDTO emailDTO, HttpServletRequest request){
        return userService.SendVerificationCode(emailDTO.getEmail(), request);
    }

    @Operation(summary = "根据ID查询用户数据")
    @PostMapping("/findCommonData")
    public Result<?> findCommonData(@RequestBody FindDataByIDDTO findDataByIDDTO, HttpServletRequest request) {
        return userService.FindDataByID(findDataByIDDTO, request);
    }

    @Operation(summary = "更新头像")
    @PostMapping("/updateAvatar")
    public Result<?> updateAvatar(@Parameter(description = "用户id", required = true) @RequestParam Integer id,
                                  @Parameter(description = "新头像", required = true) @RequestParam("file") MultipartFile file,
                                  HttpServletRequest request) throws IOException {
        UpdateAvatarDTO updateAvatarDTO = new UpdateAvatarDTO();
        updateAvatarDTO.setId(id);
        updateAvatarDTO.setAvatarFile(file);
        return userService.UpdateAvatar(updateAvatarDTO, request);
    }

    @Operation(summary = "上传图片测试")
    @PostMapping("/postTest")
    public Result<?> postTest(MultipartFile file) throws IOException {
        String url = aliOSSUtils.upload(file, "classtopia-avatar");
        return Result.success(url);
    }

    @Operation(summary = "用户退出登录")
    @PostMapping("/logout")
    public Result<?> logout(@Parameter(description = "用户id", required = true) @RequestParam Long id
            , HttpServletRequest request) {
        return userService.Logout(id, request);
    }
}
